//*******************************************************************************
// COPYRIGHT NOTES
// ---------------
// This is a sample for BCGControlBar Library Professional Edition
// Copyright (C) 1998-2016 BCGSoft Ltd.
// All rights reserved.
//
// This source code can be used, distributed or modified
// only under terms and conditions 
// of the accompanying license agreement.
//*******************************************************************************
//
// ChartObjectsView.cpp : implementation file
//

#include "stdafx.h"
#include "bcgpchartexample.h"
#include "ChartObjectsView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#ifndef M_PI
	#define M_PI 3.14159265358979323846
#endif

class CChartImageObject : public CBCGPChartObject
{
	DECLARE_DYNCREATE(CChartImageObject);

public:
	CChartImageObject()
	{
		m_bIsForeground = FALSE;
	}

	CChartImageObject(CBCGPChartVisualObject* pParentChart, const CBCGPRect& rectImage /* values */, const CBCGPImage& image)
	{
		m_pParentChart = pParentChart;
		m_Image = image;
		m_bIsForeground = FALSE;

		SetCoordinates(rectImage, CBCGPChartObject::CM_CHART_VALUES);
	}

protected:
	virtual void OnDraw(CBCGPGraphicsManager* pGM, const CBCGPRect& /*rectDiagram*/)
	{
		if (!m_rectScreen.IsRectNull() && !m_Image.IsEmpty() && IsVisible())
		{
			CBCGPRect rectDraw = m_rectScreen;
			rectDraw.Normalize();

			pGM->DrawImage(m_Image, rectDraw.TopLeft(), rectDraw.Size(), 0.75);
		}
	}

	CBCGPImage m_Image;
};

IMPLEMENT_DYNCREATE(CChartImageObject, CBCGPChartObject)

/////////////////////////////////////////////////////////////////////////////
// CChartObjectsView

IMPLEMENT_DYNCREATE(CChartObjectsView, CBCGPChartExampleView)

CChartObjectsView::CChartObjectsView()
	: CBCGPChartExampleView(CChartObjectsView::IDD)
{
	//{{AFX_DATA_INIT(CChartObjectsView)
	m_bColoredZones = TRUE;
	m_bCustomLines = TRUE;
	m_bInterlineColorZones = TRUE;
	m_bTextObjects = TRUE;
	m_bCustomImage = TRUE;
	//}}AFX_DATA_INIT

	m_pRangeHorz = NULL;
	m_pRangeVert = NULL;

	m_pDummyGreenSeries = NULL;
	m_pDummyRedSeries = NULL;

	m_pEffect_0_1 = NULL;
	m_pEffect_2_0 = NULL;

	m_pLine1 = NULL;
	m_pLine2 = NULL;
	m_pLine3 = NULL;

	m_pMarkLine1 = NULL;
	
	m_pTextObjectGreen = NULL;
	m_pTextObjectRed = NULL;
	m_pTextObjectCaption = NULL;
	
	m_pImageObject = NULL;

	m_strokeStyle.SetDashStyle(CBCGPStrokeStyle::BCGP_DASH_STYLE_DASH_DOT);

	SetObjectColors();
}

CChartObjectsView::~CChartObjectsView()
{
}

void CChartObjectsView::SetObjectColors()
{
	m_brLine1 = CBCGPBrush(m_bIsDarkBackground ? CBCGPColor::PaleVioletRed : CBCGPColor::DarkRed);
	m_brLine2 = CBCGPBrush(m_bIsDarkBackground ? CBCGPColor::LightBlue : CBCGPColor::DeepSkyBlue);
	m_brLine3 = CBCGPBrush(m_bIsDarkBackground ? CBCGPColor::Green : CBCGPColor::ForestGreen);
	m_brFill1 = CBCGPBrush(CBCGPColor::LightGreen, .2);
	m_brFill2 = CBCGPBrush(CBCGPColor::Orange, .2);
	m_brAxisMarkFill = CBCGPBrush(CBCGPColor::Orchid, m_bIsDarkBackground ? .6 : .2);
	m_brFillText1 = CBCGPBrush(CBCGPColor::White, .7);
	m_brFillText2 = CBCGPBrush(CBCGPColor::White, .7);
	m_brFillText3 = CBCGPBrush(CBCGPColor::LightSkyBlue, CBCGPColor::White, CBCGPBrush::BCGP_GRADIENT_DIAGONAL_LEFT, m_bIsDarkBackground ? .8 : .4);
	m_brText1 = CBCGPBrush(CBCGPColor::ForestGreen);
	m_brText2 = CBCGPBrush(CBCGPColor::DarkRed);
	m_brText3 = CBCGPBrush(CBCGPColor::SteelBlue);

	if (m_pLine1 != NULL)
	{
		m_pLine1->m_format.m_outlineFormat.m_brLineColor = m_brLine1;
	}

	if (m_pLine2 != NULL)
	{
		m_pLine2->m_format.m_outlineFormat.m_brLineColor = m_brLine2;
	}

	if (m_pLine3 != NULL)
	{
		m_pLine3->m_format.m_outlineFormat.m_brLineColor = m_brLine3;
	}

	if (m_pTextObjectCaption != NULL)
	{
		m_pTextObjectCaption->m_format.m_brFillColor = m_brFillText3;
	}

	if (m_pMarkLine1 != NULL)
	{
		m_pMarkLine1->m_format.m_brFillColor = m_brAxisMarkFill;
	}
}

void CChartObjectsView::DoDataExchange(CDataExchange* pDX)
{
	CBCGPChartExampleView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChartObjectsView)
	DDX_Control(pDX, IDC_CHART, m_wndChart);
	DDX_Check(pDX, IDC_COLORED_ZONES, m_bColoredZones);
	DDX_Check(pDX, IDC_CUSTOM_LINES, m_bCustomLines);
	DDX_Check(pDX, IDC_INTERLINE_COLOR_ZONES, m_bInterlineColorZones);
	DDX_Check(pDX, IDC_TEXT_OBJECTS, m_bTextObjects);
	DDX_Check(pDX, IDC_CUSTOM_IMAGE, m_bCustomImage);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CChartObjectsView, CBCGPChartExampleView)
	//{{AFX_MSG_MAP(CChartObjectsView)
	ON_BN_CLICKED(IDC_TEXT_OBJECTS, OnUpdateChart)
	ON_BN_CLICKED(IDC_COLORED_ZONES, OnUpdateChart)
	ON_BN_CLICKED(IDC_CUSTOM_LINES, OnUpdateChart)
	ON_BN_CLICKED(IDC_INTERLINE_COLOR_ZONES, OnUpdateChart)
	ON_BN_CLICKED(IDC_CUSTOM_IMAGE, OnUpdateChart)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChartObjectsView diagnostics

#ifdef _DEBUG
void CChartObjectsView::AssertValid() const
{
	CBCGPChartExampleView::AssertValid();
}

void CChartObjectsView::Dump(CDumpContext& dc) const
{
	CBCGPChartExampleView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CChartObjectsView message handlers

void CChartObjectsView::OnInitialUpdate() 
{
	CBCGPChartExampleView::OnInitialUpdate();
	
	if (m_bIsReady)
	{
		return;
	}

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	CBCGPChartSeries* pSeries1 = pChart->CreateSeries(_T("Series 1"));
	pSeries1->SetCurveType (BCGPChartFormatSeries::CCT_SPLINE);

	CBCGPChartSeries* pSeries2 = pChart->CreateSeries(_T("Series 2"));
	pSeries2->SetCurveType (BCGPChartFormatSeries::CCT_SPLINE_HERMITE);

	CBCGPChartSeries* pSeries3 = pChart->CreateSeries(_T("Series 3"));
	pSeries3->SetCurveType (BCGPChartFormatSeries::CCT_SPLINE_HERMITE);

	const int nTotal = 10;

	for (int i = 0; i < nTotal; i++)
	{
		pSeries1->AddDataPoint(3 * sin(M_PI * 4 * i / (nTotal - 1)));
		pSeries2->AddDataPoint(exp((.2 * i)));
		pSeries3->AddDataPoint(-exp((.2 * i)));
	}
	
	// Create color ranges
	m_pRangeHorz = pChart->AddChartRangeObject(2, 6, TRUE /* Horizontal */, m_brFill1);
	m_pRangeVert = pChart->AddChartRangeObject(6, 9, FALSE /* Vertical */, m_brFill2);

	// Create interline coloring effects between series lines
	m_pEffect_0_1 = new CBCGPChartInterLineColoringEffect(pChart, pChart->GetSeries(0), pChart->GetSeries(1));
	m_pEffect_0_1->m_brTopBrush.SetColor(CBCGPColor::DodgerBlue, 0.5);
	pChart->AddChartEffect(m_pEffect_0_1);

	m_pEffect_2_0 = new CBCGPChartInterLineColoringEffect(pChart, pChart->GetSeries(2), pChart->GetSeries(0));
	m_pEffect_2_0->m_brTopBrush.SetColor(CBCGPColor::Red, 0.5);

	pChart->AddChartEffect(m_pEffect_2_0);

	// Create custom line object objects

	// Horz Line
	m_pLine1 = pChart->AddChartLineObject(5.0, TRUE /* Horizontal */, m_brLine1, 2);

	// Vert line
	m_pLine2 = pChart->AddChartLineObject(6.5, FALSE /* Vertical */, m_brLine2, 2);

	// Diagonal line
	m_pLine3 = pChart->AddChartLineObject(1., -1., 10., -6., m_brLine3, 3., &m_strokeStyle);

	m_pMarkLine1 = pChart->AddChartAxisMarkObject(5.0, _T("5.0"), TRUE, TRUE, CBCGPBrush(), m_brAxisMarkFill);

	// Create text objects
	m_pTextObjectGreen = new CBCGPChartTextObject(pChart, _T("Green Area"), 2, 3, m_brText1, m_brFillText1, m_brLine3,
		30, -45, TRUE);
	pChart->AddChartObject(m_pTextObjectGreen);

	m_pTextObjectRed = new CBCGPChartTextObject(pChart, _T("Red Area"), 10, -6, m_brText2, m_brFillText2, m_brLine1);
	pChart->AddChartObject(m_pTextObjectRed);

	m_pTextObjectCaption = new CBCGPChartTextObject(pChart, _T("BCGSoft Chart\nDescription"), 1, -4, m_brText3, m_brFillText3, m_brText3);
	
	m_pTextObjectCaption->m_format.m_textFormat = CBCGPTextFormat(
		_T("Arial"), 20., FW_REGULAR, CBCGPTextFormat::BCGP_FONT_STYLE_ITALIC);

	m_pTextObjectCaption->m_format.SetContentPadding(CBCGPSize(10., 10.));
	m_pTextObjectCaption->SetRoundedShape();
	m_pTextObjectCaption->SetShadow();

	pChart->AddChartObject(m_pTextObjectCaption);

	// Create dummy area series to display colored zone keys in the legend
	m_pDummyGreenSeries = pChart->CreateSeries("Green Band", m_brFill1.GetColor(), BCGP_CT_DEFAULT, BCGPChartArea);
	m_pDummyRedSeries = pChart->CreateSeries("Red Band", m_brFill2.GetColor(), BCGP_CT_DEFAULT, BCGPChartArea);
	
	// area charts automatically set "display data between tick marks to FALSE;
	// return it back to TRUE for consistency.
	pChart->GetChartAxis(BCGP_CHART_X_PRIMARY_AXIS)->SetDisplayDataBetweenTickMarks(TRUE);

	// Create custom image object:

	m_pImageObject = new CChartImageObject(pChart, CBCGPRect(4, 2.6, 5, 4.6), CBCGPImage(IDB_IMAGE));
	pChart->AddChartObject(m_pImageObject);

	m_bIsReady = TRUE;

	SetDefaultLineWidth();
	OnUpdateChart();
}

void CChartObjectsView::OnUpdateChart() 
{
	if (!m_bIsReady)
	{
		return;
	}

	UpdateData();

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	m_pRangeHorz->SetVisible(m_bColoredZones);
	m_pRangeVert->SetVisible(m_bColoredZones);

	if (m_pDummyGreenSeries != NULL)
	{
		m_pDummyGreenSeries->m_bIncludeSeriesToLegend = m_bColoredZones;
	}

	if (m_pDummyRedSeries != NULL)
	{
		m_pDummyRedSeries->m_bIncludeSeriesToLegend = m_bColoredZones;
	}

	m_pEffect_0_1->SetVisible(m_bInterlineColorZones);
	m_pEffect_2_0->SetVisible(m_bInterlineColorZones);
	
	m_pLine1->SetVisible(m_bCustomLines);
	m_pLine2->SetVisible(m_bCustomLines);
	m_pLine3->SetVisible(m_bCustomLines);
	m_pMarkLine1->SetVisible(m_bCustomLines);

	m_pTextObjectGreen->SetVisible(m_bTextObjects);
	m_pTextObjectRed->SetVisible(m_bTextObjects);
	m_pTextObjectCaption->SetVisible(m_bTextObjects);

	m_pImageObject->SetVisible(m_bCustomImage);

	pChart->SetDirty();
	pChart->Redraw();
}
